﻿//----------------------------------------------------------------
// Desc: windows窗体
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2012-04-04
//
// Update Date : 
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


#if FUEL_WND_WINDOWS


class FWnd_windows : public IFWnd
{
    FDECLARE_CLASS(FWnd_windows)

public:
    FWnd_windows();
    virtual~FWnd_windows();

    virtual bool GetActive() { return nActive != WA_INACTIVE; }
    virtual bool GetMinimized() { return bIsMinimized; }
    virtual HWND GetHwnd();
    virtual HINSTANCE GetHinstance();
    virtual bool Initialize(FEngineParameter* param);
    virtual bool Run();
    virtual void Release();
    virtual void __GetCliRect(RECT& rc);
    virtual void __GetCurPos(POINT& pt);
    virtual void __ReSize(WORD w, WORD h, bool center);
    virtual void __ToWndCenter(RECT& rc);
    virtual void __Quit();
    virtual void __DeleteMe();

    // 消息回调函数
    friend LRESULT CALLBACK FFuelWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
    // 消息处理函数
    bool MsgProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

    FINPUT_PACKAGE inputPack;   // 输入事件转发包
    DWORD dwWindowStyle;        // 窗口样式
    BYTE nActive;               // 窗口激活状态
    bool bIsMinimized;          // 窗口是否已经最小化
    TCHAR szClass[32];          // 窗口类名

protected:
    struct FCursorDragCaption 
    {
        FCursorDragCaption()
            :bCaptionDown(false)
        {
            nLstFramePoint.x = -123;
        }
        ~FCursorDragCaption(){}

        HWND hwnd;
        bool bCaptionDown;
        POINT nOldCursorPoint;
        RECT nOldWndRect;
        POINT nLstFramePoint;

        bool IsDraging(){ return bCaptionDown; }
        void BeginDrag()
        {
            GetCursorPos(&nOldCursorPoint);
            GetWindowRect(hwnd, &nOldWndRect);
            bCaptionDown = true;
        }
        void EndDrag(){ bCaptionDown = false; }
        void DragProcess()
        {
            if (!bCaptionDown)
                return;

            POINT nNewCursorPoint;
            GetCursorPos(&nNewCursorPoint);
            if (nLstFramePoint.x != nNewCursorPoint.x || nLstFramePoint.y != nNewCursorPoint.y)
            {
                nNewCursorPoint.x = nOldWndRect.left + nNewCursorPoint.x - nOldCursorPoint.x;
                nNewCursorPoint.y = nOldWndRect.top + nNewCursorPoint.y - nOldCursorPoint.y;
                MoveWindow(hwnd, nNewCursorPoint.x, nNewCursorPoint.y, 
                    nOldWndRect.right - nOldWndRect.left,  nOldWndRect.bottom - nOldWndRect.top, TRUE);
                nLstFramePoint = nNewCursorPoint;
            }

            if (!FIsKeyDown(0, VK_LBUTTON))
                EndDrag();
        }
    } nCursorDrag;
};


#endif